package com.baturu.vin.benz.dal.dao;

import com.baturu.vin.benz.dto.BenzImageDTO;
import com.baturu.vin.benz.dto.BenzImgPositionDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;

/**
 * 奔驰vin码和配件编码对应图片DAO
 *
 * @author 蒋鑫磊
 * @Time 2016/11/28.
 */
public interface BenzImageDAO {

    @Select({"<script> select IMGTYPE,SEQNO,RESTIMG,SUBGRP,ARCHIVED,GROUPNUM from BM_NPG_V " +
            "where CATNUM=#{catnum} " +
            "AND GROUPNUM=#{groupnum} " +
            "AND SUBGRP=#{subgrp}" +
            "AND CALLOUT LIKE CONCAT('%',#{callout},'%') " +
            "ORDER BY SEQNUM  limit 1</script>"})
    BenzImageDTO findByPartsCode(@Param("catnum") String catnum, @Param("groupnum") String groupnum, @Param("subgrp") String subgrp, @Param("callout") String callout);

    @Select({"<script> select CATNUM,IMGTYPE,SEQNO,RESTIMG,SUBGRP,ARCHIVED,GROUPNUM,CALLOUT from BM_NPG_V ",
            "where (catnum, groupnum, subgrp) in ",
            "<foreach item='item' index='index' collection='dtos' open='(' separator=',' close=')'>",
            "(#{item.catnum}, #{item.groupnum}, #{item.subgrp})",
            "</foreach>",
            "</script>"})
    List<BenzImageDTO> findByPartsCodeByQueryParam(@Param("dtos") Collection<BenzImageDTO> dtos);

    @Select({"select imgName, callout, topLeftX, topLeftY, bottomRightX, bottomRightY from benz_callout_position ",
            "where imgName = #{imgName} ",
            "and callout = #{callout} limit 1"})
    List<BenzImgPositionDTO> findImgPosition(@Param("imgName") String imgName, @Param("callout") String callout);

    @Select({"<script>select distinct imgName, callout, topLeftX, topLeftY, bottomRightX, bottomRightY from benz_callout_position ",
            "where (imgName, callout) in",
            "<foreach item='item' index='index' collection='dtos' open='(' separator=',' close=')'>",
            "(#{item.imgName}, #{item.callout})",
            "</foreach>",
            "</script>"})
    List<BenzImgPositionDTO> findImgPositionByQueryParam(@Param("dtos") List<BenzImgPositionDTO> dtos);

    @Insert({"insert into benz_callout_position_newdata(imgName,callout,topLeftX,topLeftY,bottomRightX,bottomRightY) values ${insertStr} "})
    int insertBenzImagePosition(@Param("insertStr") String insertStr);


    @Select({"select distinct imgName from tmp_benz_missing_image"})
    List<String> getMissingPic();

    @Select({"<script>select distinct imgtype, groupnum, subgrp, seqno, restimg from bm_npg_v",
            "where catnum in ",
            "<foreach item='item' index='index' collection='catnum' open='(' separator=',' close=')' >",
            "#{item}",
            "</foreach>",
            "</script>"})
    List<BenzImageDTO> queryImageCodeByCatNums(@Param("catnum") Collection<String> catnum);

    @Select({"<script>select distinct catnum, imgtype, groupnum, subgrp, seqno, restimg, callout from bm_npg_v",
            "where catnum in ",
            "<foreach item='item' index='index' collection='catnum' open='(' separator=',' close=')' >",
            "#{item}",
            "</foreach>",
            "<if test='groupNos!=null and groupNos.size!=0'>",
            "AND groupnum in ",
            "<foreach item='groupNo' index='index' collection='groupNos' open='(' separator=', ' close=') '>",
            "#{groupNo}",
            "</foreach>",
            "</if>",
            "</script>"})
    List<BenzImageDTO> queryImageCode(@Param("catnum") Collection<String> catnum, @Param("groupNos") Collection<String> groupNos);

}